package com.yangleping.springframework.beans;

import java.util.ArrayList;
import java.util.List;

public class PropertyValues {

    private final List<PropertyValue> propertyValueList = new ArrayList<>();

    public void addPropertyValue(PropertyValue pv) {
        propertyValueList.add(pv);
    }

    public void addOrUpdatePropertyValue(PropertyValue pv) {
        for (int i = 0; i < propertyValueList.size(); i++) {
            PropertyValue existingPv = propertyValueList.get(i);
            if (existingPv.getName().equals(pv.getName())) {
                // 替换现有属性值
                propertyValueList.set(i, pv);
                return;
            }
        }
        // 如果没有找到同名属性，则添加新的属性值
        propertyValueList.add(pv);
    }

    public PropertyValue getPropertyValue(String propertyName) {
        for (PropertyValue pv : propertyValueList) {
            if (pv.getName().equals(propertyName)) {
                return pv;
            }
        }
        return null;
    }

    public List<PropertyValue> getPropertyValues() {
        return this.propertyValueList;
    }
}
